package main

import (
	"fmt"
	"time"
)

//通道
func main() {
	//test1()
	//chanDemo()
	bufferedChannel()
}

func chanDemo() {
	c := make(chan int)
	go worker(c)
	c <- 1
	c <- 2

}

func worker(c chan int) {
	for {
		n := <-c
		fmt.Println(n)
	}
}

func test1() {
	start := time.Now()

	chs := make([]chan int, 10)

	for i := 0; i < 10; i++ {
		chs[i] = make(chan int)
		go add(1, i, chs[i])
	}

	for _, ch := range chs {
		<-ch
	}

	encome := time.Now().Sub(start).Nanoseconds()
	fmt.Println("程序执行耗时(s)", encome)
}

func add(a int, b int, ch chan int) {

	c := a + b
	ch <- 1
	fmt.Printf("%v + %v = %v \n", a, b, c)
}

func chanTest() {
	ch := make([]chan int, 10)
	for _, element := range ch {
		fmt.Printf("通道内的参数是 %v \n", element)
	}
}

func bufferedChannel() {
	fmt.Println("缓存通道")
	c := make(chan int, 3)
	c <- 1
	c <- 2
	c <- 3
	close(c)

}
